package com.example.demo.service.Impl;

import com.example.demo.ao.ApprovalAo;
import com.example.demo.ao.ApprovalRecordAo;
import com.example.demo.ao.User;
import com.example.demo.mapper.ApprovalMapper;
import com.example.demo.service.ApprovalService;
import com.example.demo.vo.ApprovalRecordVo;
import com.example.demo.vo.ApprovalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApprovalServiceImpl implements ApprovalService {




    @Autowired
    private ApprovalMapper approvalMapper;


    @Override
    public String insertApprovalInfo(ApprovalAo approvalAo) {
        int result = 0;
        //提交审批
        if (approvalAo.getId() == null) {
            if ("00".equals(approvalAo.getButtonType())) {
                //待审批
                approvalAo.setStatus("01");
                approvalAo.setCurrentStage(1);
            } else if ("01".equals(approvalAo.getButtonType())) {
                //草稿
                approvalAo.setStatus("00");
            }else {
                //撤回or删除
                approvalAo.setStatus("04");
                approvalAo.setUpdateBy(approvalAo.getUserId());
                result = approvalMapper.updateLeaveApprovalById(approvalAo);
            }
            approvalAo.setCreateTime(new Date());
            approvalAo.setUpdateBy(approvalAo.getUserId());
            approvalAo.setCreateBy(approvalAo.getUserName());
            result = approvalMapper.insertApprovalInfo(approvalAo);
        }else {
            if ("00".equals(approvalAo.getButtonType())) {
                approvalAo.setStatus("01");
                approvalAo.setCurrentStage(1);
            }else if ("01".equals(approvalAo.getButtonType())) {
                approvalAo.setStatus("00");
            }else {
                ApprovalRecordAo approvalRecordAo = new ApprovalRecordAo();
                approvalRecordAo.setCreateTime(new Date());
                approvalRecordAo.setLeaveId(approvalAo.getId());
                approvalRecordAo.setApprovalResult("02");
                approvalRecordAo.setComments(approvalAo.getReason());
                approvalRecordAo.setCreateTime(new Date());
                approvalRecordAo.setArchiveTime(new Date());


                result = approvalMapper.insertApprovalRecord(approvalRecordAo);
            }
            return result > 0 ? "用户重新发起申请" : "更新失败";
        }
        if (result > 0 ){
            return "发起成功";
        }else {
            return "操作失败";
        }

    }




    @Override
    public String insertApprovalLow(ApprovalAo approvalAo, User user) {
        int result = 0;
        User userInfo = approvalMapper.selectApprovalUserById(user.getUserId());
        if (userInfo.getType().equals("0") || userInfo.getType().equals("1")) {
            approvalAo.setHandoverPersonId(userInfo.getUserId());
            approvalAo.setCurrentStage(1);
        } else if (userInfo.getType().equals("2")) {
            approvalAo.setApprovalPerson1Id(userInfo.getUserId());
            approvalAo.setCurrentStage(2);

        } else if (userInfo.getType().equals("3")) {
            approvalAo.setApprovalPerson1Id(userInfo.getUserId());
            approvalAo.setCurrentStage(3);
        }else if (userInfo.getType().equals("4")) {
            approvalAo.setApprovalPerson2Id(userInfo.getUserId());
            approvalAo.setCurrentStage(4);
        }
        ApprovalVo approvalVo = approvalMapper.selectApproval(approvalAo);
        if (approvalVo == null){
            return "无需审批";
        }else {
            //同意
            if ("01".equals(approvalAo.getButtonType()) && !userInfo.getType().equals("4")) {
                approvalAo.setStatus("01");
                approvalAo.setCurrentStage(approvalVo.getCurrentStage() + 1);
                approvalAo.setUpdateTime(new Date());
                approvalAo.setUpdateBy(approvalAo.getUserId());
                result = approvalMapper.updateLeaveApprovalById(approvalAo);
            }else if ("01".equals(approvalAo.getButtonType())) {
                ApprovalRecordAo approvalRecordAo = new ApprovalRecordAo();
                approvalRecordAo.setLeaveId(approvalAo.getId());
                approvalRecordAo.setApproverId(approvalAo.getUserId());
                approvalRecordAo.setApprovalRole(approvalAo.getUserName());
                approvalRecordAo.setApprovalResult("01");
                approvalRecordAo.setComments(approvalAo.getReason());
                approvalRecordAo.setCreateTime(new Date());
                approvalRecordAo.setApprovalTime(new Date());
                result = approvalMapper.insertApprovalRecord(approvalRecordAo);
            }

            return result > 0 ? "审批成功" : "审批失败";
        }

    }


    //查询历史记录
    @Override
    public List<ApprovalVo> getApprovalInfoByUserId(Integer userId) {
        return approvalMapper.getApprovalInfoByUserId(userId);
    }

    @Override
    public List<ApprovalRecordVo> getApprovalRecord(Integer userId) {
        return approvalMapper.getApprovalRecord(userId);
    }


}
